package connect;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Client extends Thread {
    static DatagramSocket clientSocket;
    static String receiveMsg = "HdlCblCcqCjw";
    String chessCoordinate = "empty";

    static int linkFinish = 0;

    // 用来广播
    public Client() throws Exception {
        // 创建发送端对象, 发送端自带默认端口号, 也可以指定端口号
        try {
            clientSocket = new DatagramSocket(6666);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static String getReceiveMsg() {
        return receiveMsg;
    }

    public String getChessCoordinate() {
        return chessCoordinate;
    }

    public void resetReceiveMsg() {
        receiveMsg = "HdlCblCcqCjw";
    }

    public static void setLinkFinish() {
        linkFinish = 1;
    }

    public int getLinkFinish() {
        return linkFinish;
    }

    public static void sendMessage(String target_IP, String message) throws Exception {
        // 创建一个数据包对象接受数据
        byte[] buffer = message.getBytes();
        //获取正确IP
        GetTrueIP getIp = new GetTrueIP();
        InetAddress trueIp = getIp.getLocalHostExactAddress();
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length, trueIp.getByName(target_IP), 6666);

        // 使用发送端send方法, 用数据包发送对象
        clientSocket.send(packet);
        System.out.println("发送成功 : " + new String(buffer, 0, packet.getLength()));
    }

    public static void callSendMessage(String target_IP, String message) {
        try {
            sendMessage(target_IP, message);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static void receiveMessage() throws Exception {
        // 创建一个数据包对象接受数据
        byte[] buffer = new byte[1024 * 64];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

        clientSocket.receive(packet);

        // 4. 根据实际接受数据的字节个数获取结果
        int len = packet.getLength();
        receiveMsg = packet.getSocketAddress().toString().replaceAll("/", "") + " : " + new String(buffer, 0, len);

        System.out.println(receiveMsg);
    }

    public static void callReceiveMessage(){
        try {
            receiveMessage();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
//    public static void receiveChessCoordinate() throws Exception {
//        // 创建一个数据包对象接受数据
//        byte[] buffer = new byte[1024 * 64];
//        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
//
//        clientSocket.receive(packet);
//
//        // 4. 根据实际接受数据的字节个数获取结果
//        int len = packet.getLength();
//        this.chessCoordinate = packet.getSocketAddress().toString().replaceAll("/", "") + " : " + new String(buffer, 0, len);
//        System.out.println("Coordinate : " + chessCoordinate);
//    }

    public static void release() {
        clientSocket.close();
    }

    @Override
    public void run() {
        try {
            while (linkFinish != 1) {
                receiveMessage();
            }
            System.out.println("连接结束");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}